#!/usr/bin/env python
# -*- coding: utf-8 -*-

from PyQt4 import QtCore, QtGui
from Ui_dialog_find_AE import *


class dialog_find_AE(QtGui.QDialog, Ui_Dialog_find_AE):
    def __init__(self,parent):
        super(dialog_find_AE,self).__init__(parent)
        
        self.setupUi(self)
        self.parent=parent
        
        #self.comboBox_find_AE.hide()
        
        self.connect(self.buttonBox, QtCore.SIGNAL("accepted()"),self, QtCore.SLOT("accept()"))
        self.connect(self.buttonBox, QtCore.SIGNAL("rejected()"),self, QtCore.SLOT("reject()"))
        
        self.lineEdit = self.comboBox_find_AE.lineEdit()
        self.connect(self.lineEdit, QtCore.SIGNAL("textEdited (const QString &)"), self.lineEdit_textEdited)
        
        
    def lineEdit_textEdited(self) :
        import re
        
        self.comboBox_find_AE.clear()        
        liste = []      
         
        texte = self.lineEdit.text()
        
        if len(self.lineEdit.text()) > 2:
            for cle in self.parent.libmyCF.ontology.all_AEs :
                if re.search(str(self.lineEdit.text()).lower(), str(self.parent.libmyCF.ontology.all_AEs[cle].name).lower()) :
                    #print self.parent.onto.all_AEs[cle].name
                    liste.append(self.parent.libmyCF.ontology.all_AEs[cle].name)
        
        index = 0
        for name in liste :
            self.comboBox_find_AE.addItem(QtCore.QString.fromUtf8(name),QtCore.QVariant(index))
            index += 1
        
        self.comboBox_find_AE.showPopup()      
                
        self.lineEdit.setText(texte)
        

    def accept(self):
        QtGui.QDialog.accept(self)